<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理</title>
    <script src="../../js/vue.js"></script>
    <script src="../../js/axios.js"></script>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        list-style-type: none;
    }

    #app {
        margin: 50px auto;
        width: max-content;
    }

    #app .content {
        width: 400px;
        height: max-content;
        line-height: 40px;
        text-align: center;
        background-color: #000;
        color: #fff;
    }

    .tr {
        overflow: hidden;
    }

    .item {
        float: left;
        width: 100px;
        height: 40px;
        cursor: context-menu;
    }

    .del:hover {
        color: coral;
    }

    .header {
        margin-bottom: 10px;
        position: relative;
    }

    .header input {
        outline: none;
    }

    .age {
        margin: 10px 0;
    }

    .add {
        position: absolute;
        bottom: 30px;
        right: 110px;
    }

    .orderCol {
        margin-right: 20px;
    }
</style>

<body>
    <div id="app">
        <div class="header">
            <div>
                <p>用户名:<input type="text" v-model="user"></p>
                <p class="userage">年&emsp;龄:<input type="text" v-model="userage"></p>
                <button class="add" @click="add()">添加</button>
                <span class="orderCol">
                    <label>id:<input type="radio" name="orderCol" value="id" v-model="sort"></label>
                    <label>年龄:<input type="radio" name="orderCol" value="userage" v-model="sort"></label>
                </span>
                <span class="orderType">
                    <label>升序:<input type="radio" name="orderType" value="asc" v-model="order"></label>
                    <label>降序:<input type="radio" name="orderType" value="desc" v-model="order"></label>
                </span>
            </div>

        </div>
        <div class="content">
            <div class="tr">
                <div class="item">编号</div>
                <div class="item">姓名</div>
                <div class="item">年龄</div>
                <div class="item">操作</div>
            </div>
            <div class="tr" v-for="item in list" :key="item.id">
                <div class="item">{{item.id}}</div>
                <div class="item">{{item.username}}</div>
                <div class="item">{{item.userage}}</div>
                <div class="item del" @click="del(item.id)">删除</div>
            </div>
        </div>
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            user: "",
            userage: "",
            sort: "id",
            order: "asc",
            list: [],
        },
        created() {
            axios({
                method: "get",
                url: "http://localhost:2345/userinfos",
            }).then(result => {
                this.list = result.data;
            })
        },
        methods: {
            async del(id) {
                // console.log(id);
                if (confirm("是否删除该数据？")) {
                    await axios({
                        method: "delete",
                        url: "http://localhost:2345/userinfos/" + id,
                    });
                    this.list = this.list.filter(function (item) {
                        return item.id != id;
                    })
                }
            },
            async add() {
                let {data: list} = await axios({
                    method: "get",
                    url: "http://localhost:2345/userinfos",
                    params: {
                        username: this.user
                    }
                });
                if (list.length) {
                    alert("该用户名已存在");
                } else {
                    let {data} = await axios({
                        method: "post",
                        url: "http://localhost:2345/userinfos",
                        data: {
                            username: this.user,
                            userage: this.userage
                        }
                    });
                    this.list.push(data);
                }
            },
            async Oder() { //排序
                let {data: list} = await axios({
                    method: "get",
                    url: "http://localhost:2345/userinfos",
                    // url: "http://localhost:2345/userinfos?_sort=" + this.sort + "&_order=" + this.order
                    params: {
                        _sort: this.sort,
                        _order: this.order
                    }
                });
                this.list = list;
            }

        },
        watch: {
            sort() {
                // this.sort = newSort;
                // console.log(this.sort);
                this.Oder();
            },
            async order() {
                // this.order = newOrder;
                this.Oder();
            }
        }
    })

</script>

</html>